//
//  WodeshoucangViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/12.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "WodeshoucangViewController.h"

#import "WodeshoucangCollectionViewCell.h"
#import "ChanpinxiangqingViewController.h"

@interface WodeshoucangViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (nonatomic, strong) UIButton * btn_right;
//
@property (nonatomic, strong) UICollectionView * collectionView_product;

//记录删除的下标
@property (nonatomic, strong) NSMutableArray * arr_index;
//数据
@property (nonatomic, strong) NSMutableArray * arr_data;


@end

@implementation WodeshoucangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    _lblTitle.text = @"我的收藏";
    _lblTitle.textColor = [UIColor whiteColor];
    _lblTitle.font = [UIFont systemFontOfSize:19];
    
    UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(15, 32, 10, 20)];
    image_return.image = [UIImage imageNamed:@"zuofanhui@2x.png"];
    [_topView addSubview:image_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 20, 35, 44);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.btn_right = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_right.frame = CGRectMake(SCREEN_WIDTH - 10 - 50, 20 + 2, 50, 40);
    self.btn_right.titleLabel.textAlignment = NSTextAlignmentRight;
    [self.btn_right setTitle:@"编辑" forState:(UIControlStateNormal)];
    [self.btn_right setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [_topView addSubview:self.btn_right];
    [self.btn_right addTarget:self action:@selector(btn_rightAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

//右编辑
- (void )btn_rightAction:(UIButton *)sender
{
    if([sender.titleLabel.text isEqualToString:@"编辑"])
    {
        [self.btn_right setTitle:@"删除" forState:(UIControlStateNormal)];
    }
    else
    {
        [self.btn_right setTitle:@"编辑" forState:(UIControlStateNormal)];
        
        NSLog(@"进行删除");
        
        self.arr_index = nil;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.collectionView_product reloadData];
    });
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    //每个item的大小
    int  item_length = (SCREEN_WIDTH - 30) / 2;
    layout.itemSize = CGSizeMake(item_length, item_length + 55);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView_product = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    self.collectionView_product.delegate = self;
    self.collectionView_product.dataSource = self;
    self.collectionView_product.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.collectionView_product];
    
    //注册
    [self.collectionView_product registerClass:[WodeshoucangCollectionViewCell class] forCellWithReuseIdentifier:@"cell_shoucang"];
}

#pragma mark - collectionView的代理
//有几个分区
- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个分区有多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WodeshoucangCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell_shoucang" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    [cell.image_product sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"cainixihuan@2x.png"]];

    if([self.btn_right.currentTitle isEqualToString:@"删除"])
    {
        //取消隐藏
        cell.btn_select.hidden = NO;
        
        cell.btn_select.tag = indexPath.item;
        [cell.btn_select addTarget:self action:@selector(btn_selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [cell.btn_select setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];
        
        for (NSString * str_index in self.arr_index)
        {
            if([str_index integerValue] == indexPath.item)
            {
                [cell.btn_select setBackgroundImage:[UIImage imageNamed:@"xuanzhong@2x.png"] forState:(UIControlStateNormal)];
            }
        }
    }
    else
    {
        cell.btn_select.hidden = YES;
    }
    
    return cell;
}

- (void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSLog(@"%ld",(long)indexPath.item);
    
    if(![self.btn_right.currentTitle isEqualToString:@"删除"])
    {
        ChanpinxiangqingViewController * chanpinxiangqingViewController = [[ChanpinxiangqingViewController alloc] init];
        [self showViewController:chanpinxiangqingViewController sender:nil];
    }
}

#pragma mark - 点击选中
- (void )btn_selectAction:(UIButton *)sender
{
    if([sender.currentBackgroundImage isEqual:[UIImage imageNamed:@"weixuanzhong@2x.png"]])
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];
        
        [self.arr_index addObject:[NSString stringWithFormat:@"%ld",sender.tag]];
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"xuanzhong@2x.png"] forState:(UIControlStateNormal)];
        
        [self.arr_index removeObject:[NSString stringWithFormat:@"%ld",sender.tag]];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.collectionView_product reloadData];
    });
}


#pragma mark - 懒加载
- (NSMutableArray * )arr_data
{
    if(_arr_data == nil)
    {
        self.arr_data = [NSMutableArray array];
    }
    
    return _arr_data;
}

- (NSMutableArray *)arr_index
{
    if(_arr_index == nil)
    {
        self.arr_index = [NSMutableArray array];
    }
    
    return _arr_index;
}








@end
